package com.sigmatrix.h5.web.dealer.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.sigmatrix.h5.components.FeignManager;
import com.sigmatrix.h5.components.IsvTokenLoader;
import com.sigmatrix.h5.entity.DisDealerAcctRegAudit;
import com.sigmatrix.h5.entity.SmsConfig;
import com.sigmatrix.h5.enums.SmsTemplate;
import com.sigmatrix.h5.feign.args.SmsRequest;
import com.sigmatrix.h5.service.DisDealerAcctRegAuditService;
import com.sigmatrix.h5.service.DisDealerService;
import com.sigmatrix.h5.service.SmsConfigService;
import com.sigmatrix.h5.utils.CommonUtils;
import com.sigmatrix.h5.web.BaseController;
import com.sigmatrix.h5.web.Result;
import com.sigmatrix.h5.web.args.DealerAuditParam;
import com.sigmatrix.h5.web.args.DealerRegListParam;
import com.sigmatrix.h5.web.args.DealerRegParam;
import com.sigmatrix.h5.web.args.ShopAuditParam;
import com.sigmatrix.h5.web.mode.AdviceMessage;
import com.sigmatrix.h5.web.mode.DealerRegMode;
import com.sigmatrix.sc.jdbc.common.lang.Page;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags="经销商注册相关接口")
@RestController
@RequestMapping("/")
public class DealerRegController extends BaseController{
	Logger logger = Logger.getLogger(DealerRegController.class);
	
	@Autowired
	private DisDealerAcctRegAuditService disDealerAcctRegAuditService;
	
	@Autowired
	private DisDealerService disDealerService;
	
	@Autowired
	private Kryo kryo;
	
	@Autowired
	private FeignManager feignManager;
	
	@Autowired
	private IsvTokenLoader isvTokenLoader;
	
	@Autowired
	private SmsConfigService smsConfigService;
	
	
	/**
	 * 
	 *@Title: validateOpenId
	 *@Description: 校验openId是否绑定过经销商
	 *@return Result<Boolean>
	 *@author MH
	 *@date 2017年11月10日 下午4:15:38
	 *@param openId
	 *@return
	 */
	@ApiOperation(value="校验openId是否绑定过经销商 true 为绑定过，false为没绑定过",response=Boolean.class)
	@GetMapping({"api/dealer/reg/validate","open/dealer/reg/validate"})
	public Result<Boolean> validateOpenId(String openId){
		if(StringUtils.isBlank(openId)) 
			return error("openId不能为空");
		boolean flag = disDealerService.validateOpenId(openId);
		return ok(flag);
	}
	
	/**
	 * 
	 *@Title: list
	 *@Description: 查询审核列表
	 *@return Result<Page<DealerRegMode>>
	 *@author MH
	 *@date 2017年11月8日 上午10:20:57
	 *@param params
	 *@return
	 */
	@ApiOperation("查询审核列表")
	@PostMapping("/dealer/reg/list")
	public Result<Page<DealerRegMode>> list(@RequestBody DealerRegListParam params){
		DisDealerAcctRegAudit vo = new DisDealerAcctRegAudit();
		if(CommonUtils.isNotEmpty(params)){
			BeanUtils.copyProperties(params, vo);
		}
		Page<DealerRegMode> page = disDealerAcctRegAuditService.findDealerRegPage(vo, params.getPageNo(), params.getPageSize());
		return ok(page);
	}
	
	/**
	 * 
	 *@Title: regist
	 *@Description: 经销商反向注册
	 *@return Result<DealerRegMode>
	 *@author MH
	 *@date 2017年11月10日 下午3:24:14
	 *@param param
	 *@return
	 */
	@ApiOperation(value="经销商注册",response=DealerRegMode.class)
	@PostMapping({"api/dealer/reg/regist","open/dealer/reg/regist"})
	public Result<DealerRegMode> regist(@RequestBody DealerRegParam param){
		DealerRegMode mode = disDealerAcctRegAuditService.regist(param);
		return ok(mode);
	}
	
	/**
	 * 
	 *@Title: audit
	 *@Description: 反向注册审核
	 *@return Result<Void>
	 *@author MH
	 *@date 2017年11月10日 下午3:29:34
	 *@return
	 */
	@ApiOperation("审核")
	@PostMapping("/dealer/reg/audit")
	public Result<Void> audit(@RequestBody DealerAuditParam param){
		disDealerAcctRegAuditService.auditDealer(param);
		return ok(null);
	}
	/**
	 * mgt门店审核feign
	 * 
	 *@Title: auditShop
	 *@Description:
	 *@return Result<Void>
	 *@author wjwei
	 *@date 2018年3月6日 下午1:59:09
	 *@param param
	 *@return
	 */
	@ApiOperation("审核门店")
	@PostMapping("shop/reg/audit")
	public Result<Void> auditShop(@RequestBody ShopAuditParam param){
		disDealerAcctRegAuditService.auditShop(param);
		return ok(null);
	}
	@RabbitListener(queues="${rabbit.bindings[0].queue}")
	public void sendMessageListener(Message message){
		Input input = new Input(message.getBody());
		AdviceMessage advice = kryo.readObject(input , AdviceMessage.class);
		input.close();
		logger.info("收到一条消息开始发短信："+advice.getPhone());
		SmsConfig config = smsConfigService.findByModuleAndEseCode(SmsTemplate.CRM_ADVICE_DEALER.toString(), advice.getEseCode());
		if(CommonUtils.isEmpty(config)) {
			logger.info(String.format("企业%s没有配置短信模板，忽略发短信", advice.getEseCode()));
			return;
		}
		String promotionCode = advice.getPromotionCode();
		SmsRequest smsRequest = new SmsRequest();
		smsRequest.setPhoneNumber(advice.getPhone());
		smsRequest.setTemplateCode(config.getTemplateCode());
		Map<String,String> map = new HashMap<String,String>();
		map.put("1",promotionCode); 
		smsRequest.setKeyValues(map);
//		String accessToken = isvTokenLoader.getIsvToken();
		String eseCode = advice.getEseCode();
//		feignManager.sendMessage(smsRequest, accessToken, eseCode);
		feignManager.sendMessageThroughApi(smsRequest, eseCode);
	}
}
